<?php
class Yun
{
	/**
	 * 封装统一的JSON输出
	 * @param number $isError 是否错误0,1
	 * @param string $msg	提示内容
	 * @param array $data	数据
	 * @param string $callback	客户端回调
	 */
	public static function ajaxReturn($isError=0,$message='',$data=array(),$callback='')
	{
		$output = array(
			'isError' => $isError,
			'message' => $message,
			'data' => $data,
			'callback' => $callback,
		);
		echo JSON::encode($output);
		exit;
	}

	public static function is_weixin()
	{
	    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
	            return true;
	    }
	    return false;
	}

	/**
	 * 如果没有自动修复地址默认状态
	 * @param array $address 用户地址列表数据
	 */
	public static function fixAddressDefault( &$address )
	{
		if ( empty($address) ) {
			return;
		}

		$def_id = 0;

		foreach($address as $value)
		{
			if ( $value['is_default'] == 1 ) {
				$def_id = $value['id'];
				break;
			}
		}

		if ( $def_id == 0 ) {
			$first = $address[0];
			$model = new IModel('address');
			$model->setData(array('is_default' => 1));
			$model->update("id = ".$first['id']);
			$address[0]['is_default'] = 1;
		}
	}

	public static function getUrl()
	{
		$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
		$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
		$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
		$relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
		return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
	}

	/**
	 * Make a request to the given URL using cURL.
	 *
	 * @param string $url to request
	 * @param array $options for cURL object
	 * @return object
	 */
	public static function curl_request($url, array $options = NULL)
	{
		$ch = curl_init($url);

		$defaults = array(
			CURLOPT_HEADER => 0,
			CURLOPT_RETURNTRANSFER => 1,
			CURLOPT_TIMEOUT => 10,
			CURLOPT_SSL_VERIFYPEER => 0,
			CURLOPT_SSL_VERIFYHOST => 0,
		);

		// Connection options override defaults if given
		curl_setopt_array($ch, (array) $options + $defaults);

		// Create a response object
		$object = new stdClass;

		// Get additional request info
		$object->response = curl_exec($ch);
		$object->error_code = curl_errno($ch);
		$object->error = curl_error($ch);
		$object->info = curl_getinfo($ch);

		curl_close($ch);

		return $object;
	}

	/**
	 * 区分IOS和安卓环境
	 */
	public static function getAppSystem()
	{
		$sys = '';
		if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
			$sys = 'ios';
		}else/* if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android'))*/{
			$sys = 'android';
		}
		return $sys;
	}
	/**
	 * 区分测试和线上的
	 */
	public static function istest()

	{
		$siteConfigObj = new Config("site_config");
		$site_config = $siteConfigObj->getInfo();
		if($site_config['url'] == 'http://v1.yunguostore.com'){
			return true;
		}
		else{
			return false;
		}
	}
	public static function getDistributorGroupID()
	{
		$siteConfigObj = new Config("site_config");
		$site_config = $siteConfigObj->getInfo();
		if(isset($site_config['distri_groupid']))
			return $site_config['distri_groupid'];
		else{
			return 0;
		}
	}

	public static function checkEnvSys()
	{
		//程序环境判断
		$env = IClient::getDevice();
		$sys = 'win';
		if ( $env == 'mobile' ) {
			//兼容式写法，初始化从$_COOKIE获取APP设定的cookie值
			//$fromapp = isset($_COOKIE['fromapp']) ? $_COOKIE['fromapp']: ICookie::get('fromapp');
			$fromapp = isset($_COOKIE['fromapp']) ? $_COOKIE['fromapp']: '';
			if ( $fromapp ) {
				//ICookie::set('fromapp',$fromapp);
				$env = 'app';
				$sys = $fromapp;
			} else {
				$env = IClient::isWechat() ? 'we' : 'wap';
				$sys = yun::getAppSystem();
			}
		}
		
		ICookie::set('env',$env);
		ICookie::set('sys',$sys);
		return array('env'=>$env,'sys'=>$sys);
	}
}